<?php
class taoguanallocate_ctl_admin_appropriation extends desktop_controller{
    var $name = "调拔单管理";
    var $workground = "storage_center";
    function index(){
        
    	$actions = array(
        		 array(
                    'label'=>'新建',
                    'href'=>'index.php?app=taoguanallocate&ctl=admin_appropriation&act=addtransfer',
                    'target'=>'_blank'
                ),
        );

        $params = array(
		   
               /* array(
                    'label'=>'导出模板',
                    'href'=>'index.php?app=taoguanallocate&ctl=admin_appropriation&act=exportTemplate',
                    'target'=>'_blank'
                ),*/
               
            
                        'title'=>'调拔单',
                        'use_buildin_new_dialog' => false,
                        'use_buildin_set_tag'=>false,
                        'use_buildin_recycle'=>false,
                        'use_buildin_export'=>false,
                        'use_buildin_import'=>false,
                        'use_buildin_filter'=>true,
                    );
                    

        /*
         * 获取操作员管辖仓库
         */
        $oBranch = &app::get('ome')->model('branch');
        $is_super = kernel::single('desktop_user')->is_super();
        
        //只要有一个仓库管理权限就显示新建调拨单按钮
        $is_new = true;
        if (!$is_super){
            $branch_ids = $oBranch->getBranchByUser(true);
            if ($branch_ids){
            	if(count($branch_ids)>1){
            		$is_new = true;
            	}
                $oApp = &$this->app->model('appropriation_items');
                $app_list = $oApp->getList('appropriation_id', array('to_branch_id'=>$branch_ids), 0,-1);
                if ($app_list)
                foreach ($app_list as $p){
                    $applist[] = $p['appropriation_id'];
                }
                if ($applist){
                    $applist = array_unique($applist);
                    $params['base_filter']['appropriation_id'] = $applist;
                }else{
                    $params['base_filter']['appropriation_id'] = 'false';
                }
            }else{
                $params['base_filter']['appropriation_id'] = 'false';
            }
        }else{
        	$branch_list = $oBranch->Get_branchlist();
        	if(count($branch_list)>1){
        		$is_new = true;
        	}
        }
                    
        if($is_new){
           $params['actions'] = $actions;
        }
        
        $this->finder('taoguanallocate_mdl_appropriation', $params);
    }
	
    function exportTemplate(){
        header("Content-Type: text/csv");  
        header("Content-Disposition: attachment; filename=allocation".date('YmdHis').".csv");  
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');  
        header('Expires:0');
        header('Pragma:public');
        $appropriationObj = &$this->app->model('appropriation');
        $title1 = $appropriationObj->exportTemplate('appropriation');
        echo '"'.implode('","',$title1).'"';
    }
    
     /*
    * 新建调拨单
    */
    function addtransfer(){
        $OBranch = &app::get('ome')->model('branch');
        $branch  = $OBranch->getList('branch_id, name','',0,-1);
        $allBranch  = $OBranch->getAllBranchs('branch_id, name');
        
        $OProducts= &app::get('ome')->model('products');
        
        /*
         * 获取操作员管辖仓库
         */
        $is_super = kernel::single('desktop_user')->is_super();
        if (!$is_super){
           $branch_list = $OBranch->getBranchByUser();
           if(count($branch_list)>1){
           		$from_branch_check = $branch_list[0]['branch_id'];
           		$to_branch_check = $branch_list[1]['branch_id'];
           }
        }else{
            $branch_list = $branch;
           if(count($branch)>1){
           		$from_branch_check = $branch[0]['branch_id'];
           		$to_branch_check = $branch[1]['branch_id'];
           }
        }
        $this->pagedata['from_branch_check'] = $from_branch_check;
        $this->pagedata['to_branch_check'] = $to_branch_check;
        
        $this->pagedata['all_branch']   = $allBranch;
        $this->pagedata['branch_list']   = $branch_list;
        $this->pagedata['is_super']   = $is_super;
        
        $this->pagedata['operator'] = kernel::single('desktop_user')->get_name();
        $this->pagedata['branch'] = $branch ;
        

        
        $appropriation_type = &app::get('ome')->getConf('taoguanallocate.appropriation_type');
        if (!$appropriation_type) $appropriation_type = 'directly';
        $this->pagedata['appropriation_type'] = $appropriation_type;
        
        $this->singlepage("admin/appropriation/transfer.html");
    }
    
	function getProducts($from_branch_id = null,$to_branch_id = null){
        $pro_id = $_POST['product_id'];
        $pro_bn= $_GET['bn'];
        
        $pro_name= $_GET['name'];
        
        if (is_array($pro_id)){
            $filter['product_id'] = $pro_id;
        }
        
        if($pro_bn){
           
           $filter = array(
               'bn'=>$pro_bn
           );           
        }
        
        if($pro_name){
            $filter = array(
               'name'=>$pro_name
           );       
        }        

        $pObj = &app::get('ome')->model('products');
        $pObj->filter_use_like = true;
        $data = $pObj->getList('product_id,bn,name,price,barcode,spec_info',$filter,0,-1);
        $pObj->filter_use_like = false;
		
        $rows = array();
        $pids = array();
        if (!empty($data)){
        	$oBranchProduct = &app::get('ome')->model('branch_product');
        	foreach ($data as $k => $item){
                $pids[] = $item['product_id'];
            }
            $from_branch_product_store = $oBranchProduct->getStoreListByBranch($from_branch_id,$pids);
            $to_branch_product_store = $oBranchProduct->getStoreListByBranch($to_branch_id,$pids);
            
            foreach ($data as $k => $item){
                $item['price'] = app::get('purchase')->model('po')->getPurchsePrice($item['product_id'], 'asc');
                $item['num'] = 0;
                $item['from_branch_num'] = isset($from_branch_product_store[$item['product_id']]) ? $from_branch_product_store[$item['product_id']] : 0;
                $item['to_branch_num'] = isset($to_branch_product_store[$item['product_id']]) ? $to_branch_product_store[$item['product_id']] : 0;
                $rows[] = $item;
            }
        }
        
       

        echo "window.autocompleter_json=".json_encode($rows);
    }
    
 	/*
     * 调拔单保存
     */
    function do_save(){
        $this->begin();
        $oAppropriation = &$this->app->model('appropriation');
        $oBranch_product = &app::get('ome')->model('branch_product');
        $from_branch_id = $_POST['from_branch_id'];
        $to_branch_id = $_POST['to_branch_id'];
        $memo = $_POST['memo'];
        $nums = $_POST['at'];
        $operator = $_POST['operator'];
        $product_id = $_POST['product_id'];
        $appropriation_type = $_POST['appropriation_type'];
        //$from_pos_id = '';
        //$to_pos_id = '';
        
        if(!$from_branch_id || !$to_branch_id){
           $this->end(false,'请选择调出仓库和调入仓库','index.php?app=taoguanallocate&ctl=admin_appropriation&act=addtransfer');
        }
        
   	    if($from_branch_id == $to_branch_id){
            if($from_branch_id[$v] == $to_branch_id[$v]){
                $this->end(false,'调出仓库和新仓库不能是同一个','index.php?app=taoguanallocate&ctl=admin_appropriation&act=addtransfer');
            }
        }
        
        foreach($nums as $product_id=>$num){
           if(intval($num)==0){
               $this->end(false,'调拨数量不可为0','index.php?app=taoguanallocate&ctl=admin_appropriation&act=addtransfer');
           }
        
           $adata[] = array('from_pos_id'=>0,'to_pos_id'=>0,'from_branch_id'=>$from_branch_id,'to_branch_id'=>$to_branch_id,'product_id'=>$product_id,'num'=>$num);
        }
        
        if(kernel::single('taoguanallocate_appropriation')->to_savestore($adata,$appropriation_type,$memo,$operator,$msg)){
        	 $this->end(true,'调拔成功!','index.php?app=taoguanallocate&ctl=admin_appropriation');
        }else{
        	 $this->end(false,'调拔失败!','',array('msg'=>$msg));
        }
       
    }
    
}
?>